package com.chuanyang.driverh.mainorder.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.utils.BitmapUtil;

import java.util.List;

/**
 * 西风多少恨，吹不散眉弯！—唐大帅
 *
 * @author 唐帅
 *         多图上传的适配器
 *         created at
 *         max 表示需要上传的图片的最大个数
 *         list 表示图片的uri地址或者路径
 */

public class UpPicAdapter extends BaseAdapter {
    private int max;
    private List<String> list;
    private Context context;
    private ViewHolder holder;
    private int lastPosition;
    public UpPicAdapter(Context context, List<String> list, int max) {
        this.context = context;
        this.list = list;
        this.max = max;
    }

    @Override
    public int getCount() {
        if (list.size() == max) {
            return max;
        } else {
            return list.size() + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
//        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

       /* if (convertView == null) {
            convertView = View.inflate(context, R.layout.act_item_uppic, null);
            holder = new ViewHolder();
            holder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_uppic);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }*/


        convertView = View.inflate(context, R.layout.act_item_uppic, null);
        holder = new ViewHolder();
        holder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_uppic);





        if (list.size() == max) { //四个图
//            BitmapUtil.showImgForNetPath( list.get(position),holder.iv_pic);
            BitmapUtil.showLocalImg( list.get(position),holder.iv_pic);
//            YncApplication.getInstance().loadImage(context, list.get(position), R.drawable.shengfeng, R.drawable.shengfeng, holder.iv_pic);

        } else {//少于四个图

            int count=getCount();

           if (position == count - 1) { //最后一个位置显示添加
                holder.iv_pic.setImageResource(R.mipmap.raise);
//                YncApplication.getInstance().loadNativeCommonImage(context, R.drawable.raise, holder.iv_pic);

            } else {//其他位置显示相册照片
                if (list.size() > 0  )
                {
                    BitmapUtil.showLocalImg( list.get(position),holder.iv_pic);
//                    YncApplication.getInstance().loadImage(context, list.get(position), R.drawable.shengfeng, R.drawable.shengfeng, holder.iv_pic);
                }

            }
        }
        lastPosition=position;
        return convertView;
    }

    class ViewHolder {
        private ImageView iv_pic;
    }
}
